package ebcs.scan.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import jplat.tools.string.StringUtil;

public class H5PaygateUtils {

	private static Logger logger = Logger.getLogger(H5PaygateUtils.class); 
	
	/**
	 * 获取请求参数封装成headmap与bodyMap
	 * @param busiCtx
	 * @return
	 */
	public static void getParamMap(HttpServletRequest request, Map<String,String> paramMap,Map<String, String> headMap, Map<String, String> bodyMap) {
		@SuppressWarnings("unchecked")
		Enumeration<String>  enumeration = request.getParameterNames();
		while (enumeration.hasMoreElements()) {
			Object object = (Object) enumeration.nextElement();
			String key = (String) object;
			String value = request.getParameter(key);

	    	/*报文头字段
	    	 Service_version
			 Sign
			 ECustId	
		     ECustName
			 Ipaddr */
	    	
	    	switch (key) {
				case "Service_version":
				case "Sign":
				case "ECustId":
				case "ECustName":
				case "Ipaddr":
					if (headMap != null) {
						headMap.put(key, StringUtil.isEmpty(value) ? "" : value);
					}
					if (paramMap != null) {
						paramMap.put(key, StringUtil.isEmpty(value) ? "" : value);
					}
					break;
				default:
					if (bodyMap != null && StringUtil.isNotEmpty(key)) {
//						//首字母小写处理
////						String headChar = key.substring(0,1);
////						String lastStr = key.substring(1);
//						bodyMap.put(headChar.toLowerCase()+lastStr, StringUtil.isEmpty(value) ? "" : value);
						bodyMap.put(key, StringUtil.isEmpty(value) ? "" : value);
					}
					if (paramMap != null && StringUtil.isNotEmpty(key)) {
						paramMap.put(key, StringUtil.isEmpty(value) ? "" : value);
					}
					break;
			}
			
		}
	}
	
	/**
	 * 获取报文体map
	 * @param busiCtx
	 * @return
	 */
	public static Map<String,String> getBodyMap(HttpServletRequest request) {
		Map<String,String> bodyMap = new HashMap<>();
		getParamMap(request, null ,null, bodyMap);
	    return bodyMap;
	}
	
	
	/**
	 * 获取报文头map
	 * @param busiCtx
	 * @return
	 */
	public static Map<String,String> getHeadMap(HttpServletRequest request) {
		Map<String,String> headMap = new HashMap<>();
		getParamMap(request, null,headMap, null);
	    return headMap;
	}
	
	/**
	 * 校验必填项
	 * @param paramMap
	 * @return
	 */
	public static boolean validRequired(String ...mustParams){
		//关键数据非空判断
		if(mustParams!=null){
			for ( String value : mustParams ){
				if ( null == value || "".equals(value.trim()) || "null".equalsIgnoreCase(value) ){
					logger.error("关键数据非空校验不通过");
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * 报文头非空校验
	 * @param paramMap
	 * @return
	 */
	public static boolean validRequiredHead(Map<String,String> headMap){
		//关键数据非空判断
		String service_version = headMap.get("Service_version");
		String Sign = headMap.get("Sign");
		String eCustId = headMap.get("ECustId");
		logger.error("head关键数据:Sign:"+Sign+";eCustId:"+eCustId+";eCustId:"+eCustId);
		return validRequired(service_version, Sign, eCustId);
	}
	
	/**
	 * 将map转为url格式
	 * @param dataMap
	 * @return
	 */
	public static String convertMap2Url(Map<String,Object> dataMap){
		StringBuffer urlBuffer = new StringBuffer();
		Set<String> keySet = dataMap.keySet();
		Iterator<String> keys = keySet.iterator();
	    try {
			while(keys.hasNext()) {
				String key = (String) keys.next();
				String value = (String) dataMap.get(key);
				if (StringUtil.isNotEmpty(value)) {
					value = URLEncoder.encode(value,"utf-8");
			    	urlBuffer.append("&");
			    	urlBuffer.append(key);
			    	urlBuffer.append("=");
			    	urlBuffer.append(value);
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	    return urlBuffer.toString();
	}
	
	
	/**
	 * 获取项目根目录
	 * @return
	 */
	public static String getProjctDir(){
		String path = H5PaygateUtils.class.getClassLoader().getResource(".").getPath();
		int end = path.length() - "WEB-INF/classes/".length();
		path = path.substring(0,end);
		System.out.println(path);
		return path;
	}
	
	public static void main(String[] args) {
		String path = getProjctDir();
		System.out.println(path);
	}
}
